package com.example.demo.runner;

import com.example.demo.constant.FileAddressConstant;
import com.example.demo.mapper.FileMetadataMapper;
import com.example.demo.model.dataobject.FileMetadataDO;
import com.example.demo.until.FileCloseTimeOption;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class FileIndexUpdateRunner implements CommandLineRunner {
    @Resource
    private FileMetadataMapper fileMetadataMapper;

    List<FileMetadataDO> fileList = new ArrayList<>(10000);

    @Override
    @Async("threadPoolTaskExecutor")
    public void run(String... args) throws Exception {
        // 指定要扫描的磁盘根目录
        Path startDir = Paths.get(FileAddressConstant.SCAN_INDEX_ADDRESS);
        Files.walkFileTree(startDir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                // 处理文件
                Long time = FileCloseTimeOption.loadPidsFromFile(new File(FileAddressConstant.OPEN_DATE_TIME_FILE));
                long fileUpdateTime = attrs.lastModifiedTime().toMillis();

                // 如果文件更新时间大于上次关闭时间，则更新文件索引
                if (fileUpdateTime > time) {
                    FileMetadataDO fileMetadataDO = FileMetadataDO.builder()
                            .fileName(file.getFileName().toString())
                            .filePath(file.toString())
                            .fileSize(attrs.size())
                            .modifiedTime(fileUpdateTime)
                            .isDirectory(false)
                            .extension(file.getFileName().toString().substring(file.getFileName().toString().lastIndexOf(".") + 1))
                            .build();
                    // 保存到集合中
                    fileList.add(fileMetadataDO);

                    if (fileList.size() == 10000) {
                        fileMetadataMapper.insertBatch(fileList);
                        fileList.clear();
                    }
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                // 处理目录
                // 处理文件
                Long time = FileCloseTimeOption.loadPidsFromFile(new File(FileAddressConstant.OPEN_DATE_TIME_FILE));
                long dirUpdateTime = attrs.lastModifiedTime().toMillis();

                // 如果文件更新时间大于上次关闭时间，则更新文件索引
                if (dirUpdateTime > time) {
                    FileMetadataDO fileMetadataDO = FileMetadataDO.builder()
                            .fileName(dir.getFileName().toString())
                            .filePath(dir.toString())
                            .fileSize(0L)
                            .modifiedTime(dirUpdateTime)
                            .isDirectory(true)
                            .extension("")
                            .build();
                    // 保存到集合中
                    fileList.add(fileMetadataDO);

                    if (fileList.size() == 10000) {
                        fileMetadataMapper.insertBatch(fileList);
                        fileList.clear();
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });

        fileMetadataMapper.insertBatch(fileList);
        fileList.clear();
    }
}
